#include <bits/stdc++.h>
// 2025/02/05
// tag: 
// Author: Zhang Muen
using namespace std;

using int64 = int64_t;

int n, m, point[351], cnt[5], dp[41][41][41][41];

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> point[i];
    for (int i = 1; i <= m; i++){
        int x;
        cin >> x;
        ++cnt[x];
    }
    dp[0][0][0][0] = point[1];
    for (int i = 0; i <= cnt[1]; i++)
        for (int j = 0; j <= cnt[2]; j++)
            for (int k = 0; k <= cnt[3]; k++)
                for (int l = 0; l <= cnt[4]; l++){
                    int sum = i * 1 + j * 2 + k * 3 + l * 4 + 1;
                    if (i)
                        dp[i][j][k][l] = max(dp[i][j][k][l], dp[i - 1][j][k][l] + point[sum]);
                    if (j)
                        dp[i][j][k][l] = max(dp[i][j][k][l], dp[i][j - 1][k][l] + point[sum]);
                    if (k)
                        dp[i][j][k][l] = max(dp[i][j][k][l], dp[i][j][k - 1][l] + point[sum]);
                    if (l)
                        dp[i][j][k][l] = max(dp[i][j][k][l], dp[i][j][k][l - 1] + point[sum]);
                }
    cout << dp[cnt[1]][cnt[2]][cnt[3]][cnt[4]];
    return 0;
}